//
//  JMRegenerationController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/30.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMRegenerationController.h"
#import "JMAlternativeView.h"
#import "JMBrandGroup.h"
#import "JMBrand.h"
#import "JMCar.h"
#import "JMDisplayKeyViewController.h"
#import "JMWriteCodeController.h"
#import "JMSearchViewController.h"
#import "JMCarViewCell.h"
#import "JMCodeGroup.h"
#import "JMCode.h"

@interface JMRegenerationController () <UITableViewDelegate, UITableViewDataSource, JMAlternativeViewDelegate>
@property (strong, nonatomic) NSArray *brandGroup; // 商标数组
@property (strong, nonatomic) NSArray *codeGroup; // 编码数组
@property (assign, nonatomic) BOOL display; // yes展示品牌再生，no展示编码再生,默认为yes
@property (weak, nonatomic) UITableView *tableView;
@property (weak, nonatomic) JMAlternativeView *alternativeView;
@property (weak, nonatomic) UIScrollView *rightView;
@end

@implementation JMRegenerationController

- (NSArray *)brandGroup {
    if (!_brandGroup) {
        _brandGroup = [JMBrandGroup brandGroupsList];
    }
    return _brandGroup;
}

- (NSArray *)codeGroup {
    if (!_codeGroup) {
        _codeGroup = [JMCodeGroup codeGroupList];
    }
    return _codeGroup;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.display = YES;
    // 添加品牌再生与编码再生
    JMAlternativeView *alternativeView = [[JMAlternativeView alloc] initWithFrame:CGRectMake(0, 64, JMPhoneWidth, 40)];
    [self.view addSubview:alternativeView];
    self.alternativeView = alternativeView;
    alternativeView.leftButton.selected = YES; // 默认为品牌再生
    alternativeView.delegate = self;
    
    if (alternativeView.leftButton.selected) {
        // 设置导航栏内容
        self.title = @"再生";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:UIBarButtonItemStylePlain target:self action:@selector(carCheckbox)];
        
        // 设置tableView
        UITableView *tableview = [[UITableView alloc] init];
        self.tableView = tableview;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.frame = CGRectMake(0, alternativeView.y + alternativeView.height, JMPhoneWidth - 20, JMPhoneHeight - alternativeView.y - alternativeView.height);
        [self.view addSubview:self.tableView];
        [self.tableView setSeparatorColor:JMColor(242, 242, 242)];
        [self.tableView setSectionIndexColor:JMColor(87, 166, 212)];
        self.tableView.showsVerticalScrollIndicator = NO;
        
        // 添加左滑手势
        UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe)];
        [self.tableView addGestureRecognizer:leftSwipe];
        [leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
        
        // 添加右滑手势
        UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe)];
        [self.tableView addGestureRecognizer:rightSwipe];
        [rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
        
        // 设置右侧的索引栏
        UIScrollView *rightView = [[UIScrollView alloc] init];
        self.rightView = rightView;
        [self.view addSubview:rightView];
        rightView.showsVerticalScrollIndicator = NO;
        rightView.frame = CGRectMake(JMPhoneWidth - 20, 71, 20, JMPhoneHeight); // y值中有7为最顶部的VIew的高度
        rightView.backgroundColor = [UIColor whiteColor];
        CGFloat letterBtnW = 20;
        CGFloat letterBtnH = (JMPhoneHeight - rightView.y)/self.brandGroup.count;
        CGFloat letterBtnX = 0;
        for (int i = 0 ; i < self.brandGroup.count; i++) {
            UIButton *letterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [rightView addSubview:letterBtn];
            letterBtn.width = letterBtnW;
            letterBtn.height = letterBtnH;
            letterBtn.x = letterBtnX;
            letterBtn.y = letterBtnH * i;
            letterBtn.tag = i + 1000;
            [letterBtn setTitle:[self.brandGroup[i] firstchar] forState:UIControlStateNormal];
            [letterBtn setTitleColor:JMColor(153, 153, 153) forState:UIControlStateNormal];
            letterBtn.titleLabel.font = [UIFont systemFontOfSize:9];
            [letterBtn addTarget:self action:@selector(letterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}


/**
 *  点击右侧索引跳转对应位置
 *
 *  @param letterBtn 索引值为按钮的tag值减去1000
 */
- (void)letterBtnClick:(UIButton *)letterBtn {
    // 获取所点目录对应的indexPath值
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:letterBtn.tag - 1000];
    
    // 让table滚动到对应的indexPath位置
    [self.tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.display) { // 品牌再生
        return self.brandGroup.count;
    } else { // 编码再生
        return self.codeGroup.count;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.display) { // 品牌再生
        JMBrandGroup *brandGroup = self.brandGroup[section];
        return brandGroup.brands.count;
    } else { // 编码再生
        JMCodeGroup *codeGroup = self.codeGroup[section];
        return codeGroup.brands.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMCarViewCell *cell = [JMCarViewCell cellWithTableView:tableView];
    
    //2 给cell内部的子控件赋值
    if (self.alternativeView.leftButton.selected) { // 品牌再生
        JMBrandGroup *brandGroup = self.brandGroup[indexPath.section];
        JMBrand *brand = brandGroup.brands[indexPath.row];
        cell.iconView.image = [UIImage imageNamed:@"baoma"];
        cell.nameLabel.x = 75;
        cell.nameLabel.text = brand.name;
    } else { // 编码再生
        JMCodeGroup *codeGroup = self.codeGroup[indexPath.section];
        JMCode *code = codeGroup.brands[indexPath.row];
        cell.iconView.image = nil;
        cell.nameLabel.x = 20;
        cell.nameLabel.text = code.name;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

//点击右侧索引表项时调用
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.display) { // 品牌再生
       return [self.brandGroup[section] firstchar];
    } else { // 编码再生
        return [self.codeGroup[section] firstchar];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *customView = [[UIView alloc] init];
    customView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] init];
    label.opaque = NO;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = JMColor(87, 166, 212);
    
    if (self.display) { // 品牌再生
        label.text = [self.brandGroup[section] firstchar];
    } else { // 编码再生
        label.text = [self.codeGroup[section] firstchar];
    }
    
    [customView addSubview:label];
    
    if (0 == section) {
        // 最顶部顶部灰色
        UIView *backView = [[UIView alloc] init];
        backView.width = self.tableView.width;
        backView.height = 7;
        backView.x = 0;
        backView.y = 0;
        backView.backgroundColor = JMColor(242, 242, 242);
        label.frame = CGRectMake(0, 17, 40, 20);
        
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 47, backView.width, 1);
        view.backgroundColor = JMColor(242, 242, 242);
        [backView addSubview:view];
        [customView addSubview:backView];
    } else {
        // 每个分组之间的颜色
        UIView *backView = [[UIView alloc] init];
        backView.width = self.tableView.width;
        backView.height = 5;
        backView.x = 0;
        backView.y = 0;
        backView.backgroundColor = JMColor(242, 242, 242);
        label.frame = CGRectMake(0, 15, 40, 20);
        
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 45, backView.width, 1);
        view.backgroundColor = JMColor(242, 242, 242);
        [customView addSubview:view];
        [customView addSubview:backView];
    }
    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (0 == section) {
        return 47;
    }
    return 45;
}

/**
 *  选择cell,跳转到下一页
 *
 *  @param tableView tableView
 *  @param indexPath cell所在的索引位置
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 返回时cell没有了背景颜色
    [tableView reloadData];
    
    if (self.alternativeView.leftButton.selected) { //品牌再生对应跳转
        JMDisplayKeyViewController *displayKeyVC = [[JMDisplayKeyViewController alloc] init];
        JMBrandGroup *brandGroup = self.brandGroup[indexPath.section];
        JMBrand *brand = brandGroup.brands[indexPath.row];
        displayKeyVC.cars = brand.models;
        displayKeyVC.title = brand.name;
        [self.navigationController pushViewController:displayKeyVC  animated:YES];
    } else { //编码再生对应跳转
        JMWriteCodeController *writeCodeVC = [[JMWriteCodeController alloc] init];
        [self.navigationController pushViewController:writeCodeVC animated:YES];
    }

}

/** 左滑显示编码再生 */
- (void)leftSwipe {
    self.alternativeView.leftButton.selected = NO;
    self.alternativeView.rightButton.selected = YES;
    [self brandORCode];
}

/** 右滑显示品牌再生 */
- (void)rightSwipe {
    self.alternativeView.leftButton.selected = YES;
    self.alternativeView.rightButton.selected = NO;
    [self brandORCode];
}

/** 显示品牌页或显示编码页 */
- (void)brandORCode {
    if (self.alternativeView.rightButton.selected) { // 编码再生重新加载数据源
        self.display = NO;
        // 隐藏rightView
        self.rightView.hidden = YES;
        self.tableView.frame = CGRectMake(0, self.alternativeView.y + self.alternativeView.height, JMPhoneWidth, JMPhoneHeight - self.alternativeView.y - self.alternativeView.height);
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(check)];
        self.tableView.width = JMPhoneWidth;
        self.rightView.hidden = YES;
        [self.tableView reloadData];
    } else {
        self.display = YES;
        // 显示rightView
        self.rightView.hidden = NO;
        self.tableView.frame = CGRectMake(0, self.alternativeView.y + self.alternativeView.height, JMPhoneWidth - 20, JMPhoneHeight - self.alternativeView.y - self.alternativeView.height);
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索"] style:UIBarButtonItemStylePlain target:self action:@selector(carCheckbox)];
        self.tableView.width = JMPhoneWidth - 20;
        self.rightView.hidden = NO;
        [self.tableView reloadData];
    }

}

#pragma mark - JMAlternativeView代理方法
- (void)alternativeView:(JMAlternativeView *)alternativeView didSelectedButton:(UIButton *)button {
    [self brandORCode];
}

/**
 *  跳转搜索页面
 */
- (void)carCheckbox {
    JMSearchViewController *searchVC = [[JMSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)check {
    
}
@end
